package com.easico.activity;

import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;

import androidx.fragment.app.Fragment;


public class FragmentContent extends Fragment {

     private TableLayout tableLayout;
    @Override
   public View onCreateView(LayoutInflater inflater, ViewGroup container,
                          Bundle savedInstanceState) {
       View root = inflater.inflate(R.layout.fragment_content, container, false);
       
       // 初始化TableLayout
       tableLayout = new TableLayout(getContext());
       tableLayout.setLayoutParams(new TableLayout.LayoutParams(
               TableLayout.LayoutParams.MATCH_PARENT,
               TableLayout.LayoutParams.WRAP_CONTENT));
       
       // 添加表头
       addTableHeader();
       
       // 添加数据行
       addTableData();
        // 将TableLayout添加到根视图
//       FrameLayout frameLayout = root.findViewById(R.id.fragment_content);
//       frameLayout.addView(tableLayout);
       
       return root;
   }
    private void addTableHeader() {
       TableRow headerRow = new TableRow(getContext());
       
       // 创建表头单元格
       String[] headers = {"应用名称", "状态", "操作"};
       for (String header : headers) {
           TextView textView = createTextView(header);
           textView.setTypeface(null, Typeface.BOLD); // 加粗
          // textView.setBackgroundResource(R.drawable.table_header_bg); // 设置背景
           headerRow.addView(textView);
       }
       
       tableLayout.addView(headerRow);
   }
    private void addTableData() {
       // 示例数据
       String[][] data = {
           {"微信", "已安装", "打开"},
           {"支付宝", "未安装", "安装"},
           {"QQ", "已安装", "打开"}
       };
        for (String[] row : data) {
           TableRow tableRow = new TableRow(getContext());
           
           // 添加应用名称
           tableRow.addView(createTextView(row[0]));
           
           // 添加状态
           TextView statusView = createTextView(row[1]);
           if (row[1].equals("已安装")) {
               statusView.setTextColor(Color.GREEN);
           } else {
               statusView.setTextColor(Color.RED);
           }
           tableRow.addView(statusView);
           
           // 添加操作按钮
           Button actionButton = new Button(getContext());
           actionButton.setText(row[2]);
           actionButton.setTextSize(12);
           actionButton.setOnClickListener(v -> handleButtonClick(row[0], row[1]));
           tableRow.addView(actionButton);
           
           tableLayout.addView(tableRow);
       }
   }
    private TextView createTextView(String text) {
       TextView textView = new TextView(getContext());
       textView.setText(text);
       textView.setPadding(16, 16, 16, 16);
       textView.setGravity(Gravity.CENTER);
       return textView;
   }
    private void handleButtonClick(String appName, String status) {
       if (status.equals("已安装")) {
           // 打开应用的逻辑
           Toast.makeText(getContext(), "正在打开 " + appName, Toast.LENGTH_SHORT).show();
       } else {
           // 安装应用的逻辑
           Toast.makeText(getContext(), "正在安装 " + appName, Toast.LENGTH_SHORT).show();
       }
   }
}